﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;
using TPMS_ScanComparison;

namespace SerialHelp
{
    public class serialp
    {
        public SerialPort sp;
        public string PortName;
        public int rate;
        //Data bits
        public int databits = 8;
        public StopBits stopBits = StopBits.One;           
        public Parity parity = Parity.None;//校验位
        public object sending = new object();

        #region 串口参数初始化
        public bool Broad_PortInitialize()
        {
            try
            {
                if (sp == null)
                    sp = new SerialPort();
                else
                    sp.Close();
                sp.PortName = PortName;
                sp.BaudRate = rate;
                sp.DataBits = databits;
                sp.Parity = parity;
                sp.StopBits = stopBits;// StopBits.One;
                sp.ReadTimeout = 100;
                if (!sp.IsOpen) sp.Open();
                
                sp.ReadTimeout = 100;
                //        COM_ERROR = false;
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        #endregion
        /// <summary>
        /// 打开串口
        /// </summary>
        /// <returns></returns>
        public bool openPort()
        {
            if (sp == null)
            {
                sp = new SerialPort();
                Broad_PortInitialize();
            }
            if (sp.IsOpen) return true;
            try
            {
                sp.Open();
                System.Windows.Forms.MessageBox.Show(sp.PortName);
                return true;
            }
            catch (Exception ex)
            {
    //            global._log.Error("打开串口" + sp.PortName + "报错：" + ex.Message);
            }
            return false;
        }

        public bool ClosPort()
        {
            if (sp == null)
            {
                return true;
            }
            if (!sp.IsOpen) return true;
            try
            {
                sp.Close();
                return true;
            }
            catch (Exception ex)
            {
 //               global._log.Error("关闭串口" + sp.PortName + "报错：" + ex.Message);
            }
            return false;
        }

        

        public byte[] Str16ToBytes(string Str16)
        {
            Str16 = Str16.Replace(" ", "");
            if ((Str16.Length % 2) != 0)
                Str16 += " ";
            byte[] returnBytes = new byte[Str16.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(Str16.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        public void sendData(string sendstr, bool savelog = false)
        {
            if (sp == null) return;            
            lock (sending)
            {
                try
                {
                    if(!sp.IsOpen) Broad_PortInitialize();
                    byte[] obuf = Str16ToBytes(sendstr);// new byte[8] { 0x02, 0x30, 0x30, 0x30, 0x38, 0x30, 0x30, 0x31 };//02 30 30 30 38 30 30 31
                    sp.DiscardInBuffer();
                    sp.DiscardOutBuffer();
                    sp.Write(obuf, 0, obuf.Length);

                    return;
                }
                catch (Exception e)
                {
   //                 global._log.Error("读写PLC状态出错：" + e.Message + sendstr);
                    return;
                }
            }
        }

        public byte[] sendDataWithRec(string sendstr, bool savelog = true)
        {
            if (sp == null) return null;
            lock (sending)
            {
                try
                {
                    if (!sp.IsOpen) Broad_PortInitialize();
                    byte[] obuf = Str16ToBytes(sendstr);// new byte[8] { 0x02, 0x30, 0x30, 0x30, 0x38, 0x30, 0x30, 0x31 };//02 30 30 30 38 30 30 31
                    return sendData(obuf,savelog); ;
                }
                catch (Exception e)
                {
                    //                 global._log.Error("读写PLC状态出错：" + e.Message + sendstr);
                    return null;
                }
            }
        }
        public byte[] sendDataWithRec(byte[] obuf, bool savelog = false)
        {
            if (sp == null) return null;
            lock (sending)
            {
                try
                {
                    if (!sp.IsOpen) Broad_PortInitialize();
            //        byte[] obuf = Str16ToBytes(sendstr);// new byte[8] { 0x02, 0x30, 0x30, 0x30, 0x38, 0x30, 0x30, 0x31 };//02 30 30 30 38 30 30 31
                    return sendData(obuf, savelog); ;
                }
                catch (Exception e)
                {
                    //                 global._log.Error("读写PLC状态出错：" + e.Message + sendstr);
                    return null;
                }
            }
        }

        public byte[] sendData(byte[] obuf, bool savelog = false)
        {
            
            string sendstr = "";
            lock (sending)
            {
                try
                {
                    if (!sp.IsOpen) Broad_PortInitialize();
                    //             byte[] obuf = Str16ToBytes(sendstr);// new byte[8] { 0x02, 0x30, 0x30, 0x30, 0x38, 0x30, 0x30, 0x31 };//02 30 30 30 38 30 30 31
                    sp.DiscardInBuffer();
                    sp.DiscardOutBuffer();
                    string str = "";
                    foreach (byte b in obuf)
                    {
                        str = str + b.ToString("X2") + " ";
                    }
                    sendstr = str;
                    if (savelog) Global.Logger.Info("发送" + str);
                    sp.Write(obuf, 0, obuf.Length);
                    int idxqty = 0;
                    int buffLen = 0;
                    if (obuf[0] == 1 && obuf[1] == 3)//批量读取数据
                    {
                        while (buffLen < obuf[5] * 2 + 3 && idxqty < 200)
                        {
                            idxqty++;
                            buffLen = sp.BytesToRead;
                            System.Threading.Thread.Sleep(20);
                        }
                    }
                    else
                    {
                        while (buffLen < 6 && idxqty < 20)
                        {
                            idxqty++;
                            //        global._log.Info("读取可读长度");
                            buffLen = sp.BytesToRead;
                            //        global._log.Info("BytesToRead:" + buffLen.ToString());
                            System.Threading.Thread.Sleep(20);
                        }

                    }
                    if (buffLen <= 0)
                    {
      //                  global._log.Info(string.Format("发送[{0}]返回空", sendstr));
                        return null;
                    }
                    byte[] readBuffer = new byte[buffLen];
                    sp.Read(readBuffer, 0, buffLen);
                    str = "";
                    foreach (byte b in readBuffer)
                    {
                        str = str + b.ToString("X2") + " ";
                    }
                    if (savelog) Global.Logger.Info("接收" + str);
                    if (readBuffer.Length < 3)
                    {
     //                   global._log.Info(string.Format("发送数据异常[{0}]返回[{1}]", sendstr, str));
                        return null;
                    }
     //               if (readBuffer[0] != obuf[0] || readBuffer[1] != obuf[1])
     //               {
     ////                   global._log.Info(string.Format("发送数据异常[{0}]返回[{1}]", sendstr, str));
     //                   return null;
     //               }
                    return readBuffer;
                }
                catch (Exception e)
                {
     //               global._log.Error("读写PLC状态出错：" + e.Message + sendstr);
                    return null;
                }
            }
        }

    }
    /// <summary>
    /// PLC类
    /// </summary>
   public class plc : serialp
    {
        bool sendresultisok = false;//发送数据到plc是否OK
        public Action<int> PlcEvent;
        string lastreceivedstr = "";
        public void ReceievdMessage(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] receievd = new byte[1000];
            Thread.Sleep(140);
            int readqty = sp.BytesToRead;
            if (readqty < 5) return;
            if (readqty > 1000) readqty = 1000;
            sp.Read(receievd, 0, readqty);
            string tempstr = "";
            sendresultisok = false;
            for (int i = 0; i < readqty; i++)
            {
                tempstr = tempstr + receievd[i].ToString("X2") + " ";
                if (i < readqty - 18 && receievd[i] == 0x01 && receievd[i + 1] == 0x17)
                {
                    if (receievd[i + 18] == 0x01)
                    {
                        sendresultisok = true;
                    }
                }
            }
            if (tempstr != lastreceivedstr)
            {
                lastreceivedstr = tempstr;
   //             global._log.Info("收到PLC："+lastreceivedstr);
            }
            if (readqty < 13) return;
            if (PlcEvent != null)
            {
                PlcEvent(receievd[12]);
            }
        }
       
        #region 三菱读写
        public bool setM(int add)
        {
            byte[] obuf = Str16ToBytes("02 37 30 30 30 38 03");
            string str1 = add.ToString("X2");
            byte[] array = System.Text.Encoding.ASCII.GetBytes(str1);
            obuf[2] = array[0];
            obuf[3] = array[1];
            if (sendData(Mitsubishi(obuf)) != null)
                return true;
            return false;
        }

        public bool ResetM(int add)
        {
            byte[] obuf = Str16ToBytes("02 38 30 30 30 38 03");
            string str1 = add.ToString("X2");
            byte[] array = System.Text.Encoding.ASCII.GetBytes(str1);
            obuf[2] = array[0];
            obuf[3] = array[1];
            if (sendData(Mitsubishi(obuf)) != null)
                return true;
            return false;
        }

        public bool setM0()
        {
            if (sendData(Mitsubishi(Str16ToBytes("02 37 30 30 30 38 03"))) != null)
                return true;
            return false;
        }

        public bool ResetM0()
        {
            if (sendData(Mitsubishi(Str16ToBytes("02 38 30 30 30 38 03"))) != null)
                return true;
            return false;
        }

        public bool setM1()
        {
            if (sendData(Mitsubishi(Str16ToBytes("02 37 30 31 30 38 03"))) != null)
                return true;
            return false;
        }

        public bool ResetM1()
        {
            if (sendData(Mitsubishi(Str16ToBytes("02 38 31 30 30 38 03"))) != null)
                return true;
            return false;
        }

        public int GX_ReadM0()
        {
            string sendstr = "02 30 30 31 30 30 30 31 03 35 35 ";//02 30 30 31 30 30 30 31 03 35 35
            byte[] senddata = Str16ToBytes(sendstr);
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length < 6) return -1;//02 30 30 03 36 33
     //       if (recdata[5] != 0x34 && recdata[5] != 0x35) return -1;
            byte ret= (byte)(recdata[2] - 0x30);
            ret = (byte)(ret & 1);
            return ret;
        }

        public bool GX_SetM0(bool value)
        {
            string sendstr = "02 37 30 30 30 38 03 30 32 ";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM1(bool value)
        {
            string sendstr = "02 37 30 31 30 38 03 30 33 ";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM2(bool value)
        {
            string sendstr = "02 37 30 32 30 38 03 30 34 ";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM3(bool value)
        {
            string sendstr = "02 37 30 33 30 38 03 30 35 ";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM4(bool value)
        {
            string sendstr = "02 37 30 34 30 38 03 30 36 ";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM10(bool value)
        {
            string sendstr = "02 37 30 41 30 38 03 31 33";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM11(bool value)
        {
            string sendstr = "02 37 30 42 30 38 03 31 34";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM12(bool value)
        {
            string sendstr = "02 37 30 43 30 38 03 31 35";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM13(bool value)
        {
            string sendstr = "02 37 30 44 30 38 03 31 36";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM14(bool value)
        {
            string sendstr = "02 37 30 45 30 38 03 31 37";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM15(bool value)
        {
            string sendstr = "02 37 30 46 30 38 03 31 38";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM16(bool value)
        {
            string sendstr = "02 37 31 30 30 38 03 30 33";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }

        public bool GX_SetM17(bool value)
        {
            string sendstr = "02 37 31 31 30 38 03 30 34";
            byte[] senddata = Str16ToBytes(sendstr);
            if (!value)
            {
                senddata[1] = 0x38;
                senddata[8]++;
            }
            byte[] recdata = sendData(senddata);
            if (recdata == null || recdata.Length != 1) return false;//02 30 30 03 36 33
            if (recdata[0] != 0x6) return false;
            return true;
        }
        #endregion

        public bool WriteNoRead(string value)
        {
            value = value + "\r\n";
      //      byte[] array = System.Text.Encoding.ASCII.GetBytes(value);
            sp.Write(value);
            return true;
        }

        public byte[] sendDataCRC(byte[] obuf)
        {            
            lock (sending)
            {
                try
                {
                    if (!sp.IsOpen) Broad_PortInitialize();
                    byte[] res = new byte[obuf.Length + 2];
                    obuf.CopyTo(res, 0);
                    int crcVal = 0;
                    crcVal = Crc16(res, res.Length - 2);
                    res[res.Length - 2] = (byte)(crcVal & 0xFF);
                    res[res.Length - 1] = (byte)(crcVal >> 8);
                    sp.DiscardInBuffer();
                    sp.DiscardOutBuffer();
                    string tempstr = "";
                    for (int i = 0; i < res.Length; i++)
                    {
                        tempstr = tempstr + res[i].ToString("X2") + " ";
                    }
       //             global._log.Info("写PLC:"+tempstr);
                    sp.Write(res, 0, res.Length);
                    return null;
                }
                catch (Exception e)
                {
   //                 global._log.Error("读PLC状态出错：" + e.Message);
                    return null;
                }
            }
        }

        public void TsendDataCRC(object _obuf)
        {
            byte[] obuf = _obuf as byte[];
            //         global._log.Info("准备发送crc数据");            
            lock (sending)
            {
                try
                {
                    if (!sp.IsOpen) Broad_PortInitialize();
                    byte[] res = new byte[obuf.Length + 2];
                    obuf.CopyTo(res, 0);
                    int crcVal = 0;
                    crcVal = Crc16(res, res.Length - 2);
                    res[res.Length - 2] = (byte)(crcVal & 0xFF);
                    res[res.Length - 1] = (byte)(crcVal >> 8);
                    sp.DiscardInBuffer();
                    sp.DiscardOutBuffer();
                    string tempstr = "";
                    for (int i = 0; i < res.Length; i++)
                    {
                        tempstr = tempstr + res[i].ToString("X2") + " ";
                    }
    //                global._log.Info("写PLC:" + tempstr);
                    sp.Write(res, 0, res.Length);
                    return ;
                }
                catch (Exception e)
                {
      //              global._log.Error("读PLC状态出错：" + e.Message);
                    return ;
                }
            }
        }
 
        #region 校验
        private static readonly byte[] aucCRCHi = {
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40
        };
        private static readonly byte[] aucCRCLo = {
            0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
            0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
            0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
            0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
            0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
            0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
            0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
            0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
            0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
            0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
            0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
            0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
            0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
            0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
            0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
            0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
            0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
            0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
            0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
            0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
            0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
            0x41, 0x81, 0x80, 0x40
        };
        /// <summary>
        /// CRC效验
        /// </summary>
        /// <param name="pucFrame">效验数据</param>
        /// <param name="usLen">数据长度</param>
        /// <returns>效验结果</returns>
        public static int Crc16(byte[] pucFrame, int usLen)
        {
            int i = 0;
            byte ucCRCHi = 0xFF;
            byte ucCRCLo = 0xFF;
            UInt16 iIndex = 0x0000;

            while (usLen-- > 0)
            {
                iIndex = (UInt16)(ucCRCLo ^ pucFrame[i++]);
                ucCRCLo = (byte)(ucCRCHi ^ aucCRCHi[iIndex]);
                ucCRCHi = aucCRCLo[iIndex];
            }
            return (ucCRCHi << 8 | ucCRCLo);
        }
        /// <summary>
        /// 三菱PLC校验位计算
        /// </summary>
        /// <param name="obuf"></param>
        /// <returns></returns>
        public byte[] Mitsubishi(byte[] obuf)
        {
            byte[] returnBytes = new byte[obuf.Length + 2];
            int sum = 0;
            for (int i = 1; i < obuf.Length; i++)
                sum += obuf[i];
            sum = sum % 256;
            string jiaoyan1 = (sum / 16).ToString("X");
            System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
            byte intAsciiCode = (byte)asciiEncoding.GetBytes(jiaoyan1)[0];
            string jiaoyan2 = (sum % 16).ToString("X");
            System.Text.ASCIIEncoding asciiEncoding2 = new System.Text.ASCIIEncoding();
            byte intAsciiCode2 = (byte)asciiEncoding.GetBytes(jiaoyan2)[0];
            for (int i = 0; i < obuf.Length; i++)
                returnBytes[i] = obuf[i];
            returnBytes[obuf.Length] = intAsciiCode;
            returnBytes[obuf.Length + 1] = intAsciiCode2;
            return returnBytes;

        }
        #endregion

    }
}
